package DAO;

import Utils.Open;
import Utils.Stock;
import Utils.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

@Repository
public class OpenDAO
{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //创建开户信息
    public void Add_Open(String UserId,String idcard,String account, String open_time) throws Exception
    {
        String sql = "Insert into Open values(?,?,?,?,?,?)";

        Object[] args = new Object[]{UserId,idcard,account,open_time,null,0};

        jdbcTemplate.update(sql,args);
    }
    //查找开户信息
    public Open Get_OpenById(String UserId) throws Exception
    {
        String sql = "Select * from Open Where userId = ?";

        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Open>(Open.class), UserId);
    }
    //获得所有未审核开户信息
    public ArrayList<Open> getAllUnPassedOpen() throws Exception
    {
        String sql = "Select * from Open Where success = 0";

        ArrayList<Open> OpenArrayList = new ArrayList<Open>();

        OpenArrayList = (ArrayList<Open>)jdbcTemplate.query(sql,BeanPropertyRowMapper.newInstance(Open.class));
        return OpenArrayList;
    }
    //通过审核
    public void PassOpen(String UserId)
    {
        String sql_1 = "Update Open set passtime = ? where UserId = ?";
        String sql_2 = "Update Open set success = 1 where UserId = ?";
        String time = String.valueOf(System.currentTimeMillis());

        Object[] args = new Object[]{time,UserId};

        jdbcTemplate.update(sql_1,args);
        jdbcTemplate.update(sql_2,UserId);
    }
}
